<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
            <el-form-item label="banner图" prop="imageUrl">
                <uploadImage dir="banner" :limit="1" :fileList="imgList" @onFilesChange="onImgsChange"></uploadImage>
            </el-form-item>
            <el-form-item label="打开页面">
                <el-input v-model="ruleForm.page"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="ruleForm.sort" placeholder="如果要添加在某一个banner后面，就填写该banner的排序值"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiEditBannerList } from "@/api/configManage.js";
import uploadImage from '@/components/uploadImage.vue'
export default {
    name: "banner_index_edit",
    components: { uploadImage },
    props: {
        list: {
            type: Array,
            required: true
        },
        editItem: {
            type: Object,
            required: false
        }
    },
    data() {
        return {
            imgList: [],
            ruleForm: {
                imageUrl: "",
                page: "",
                sort: "",
            },
            rules: {
                imageUrl: [{ required: true, message: "请上传banner图", trigger: "change" }],
                sort: [{ required: true, message: "请输入排序", trigger: "change" }],
            },
            loading: false,
        }
    },
    created() {
        this.setForm()
    },
    watch: {
        editItem() {
            this.setForm()
        }
    },
    methods: {
        setForm() {
            const { id, imageUrl, page, sort } = this.editItem
            this.ruleForm = { id, imageUrl, page, sort }
            this.imgList = [{ url: imageUrl }]
        },
        onImgsChange(keyList) {
            this.ruleForm.mainImage = keyList.join()
        },
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    const arr = this.list
                    for (let index = 0; index < arr.length; index++) {
                        if (arr[index].id == this.ruleForm.id) {
                            arr[index] = this.ruleForm
                        }
                    }
                    const sortArr = arr.sort((a, b) => a.sort - b.sort).map((item) => {
                        return {
                            imageUrl: item.imageUrl.replaceAll("https://oss.paopaofu.com/", ""),
                            page: item.page
                        }
                    })
                    apiEditBannerList(sortArr).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>